package com.weixing.mall.provider.controller.ads;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.weixing.mall.base.constant.DatePattern;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.vo.AgentVo;
import com.weixing.mall.provider.api.service.AgentServiceRpc;
import com.weixing.mall.provider.model.domain.MediaOrderdRecord;
import com.weixing.mall.provider.model.dto.ads.MediaOrderdRecordDto;
import com.weixing.mall.provider.model.dto.ads.MediaOrderdRecordStatusDto;
import com.weixing.mall.provider.model.enums.AppointmentEunm;
import com.weixing.mall.provider.model.query.MediaOrderRecordQuery;
import com.weixing.mall.provider.model.query.MediaOrderRecordsQuery;
import com.weixing.mall.provider.model.vo.ads.MediaOrderRecordDetilVo;
import com.weixing.mall.provider.model.vo.ads.MediaOrderRecordsVo;
import com.weixing.mall.provider.model.vo.excel.MediaOrderRecordsED;
import com.weixing.mall.provider.service.IMediaOrderdRecordService;
import com.weixing.mall.provider.support.excel.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wx
 * @Date 2019/9/5 17:52
 * @Version 1.0
 */
@RestController
@RequestMapping("mediaOrderRecord")
@Api(value = "媒体广告Api",tags = "媒体广告")
public class MediaOrderRecordController extends BaseController {

    @Autowired
    private IMediaOrderdRecordService mediaOrderdRecorService;
    @Autowired
    private AgentServiceRpc agentServiceRpc;

    @ApiOperation(value = "加载媒体广告预约记录列表")
    @PostMapping("")
    public Result list(@SysInfo SysAuth auth, MediaOrderRecordsQuery mediaOrderRecordsQuery){
        return ResultUtil.success(mediaOrderdRecorService.selectOfPage(initMpPage(),mediaOrderRecordsQuery,auth.getUid()));
    }

    @ApiOperation(value = "加载媒体广告预约记录列表")
    @GetMapping("listExcel")
    public void listExcel(@SysInfo SysAuth auth, MediaOrderRecordsQuery mediaOrderRecordsQuery, HttpServletResponse response){
        IPage<MediaOrderRecordsVo> list = mediaOrderdRecorService.selectOfPage(initMpPage(),mediaOrderRecordsQuery,auth.getUid());
        List<MediaOrderRecordsED> list2 = Lists.newArrayList();
        list.getRecords().forEach(m->{
            MediaOrderRecordsED ma = new MediaOrderRecordsED();
            BeanUtils.copyProperties(m,ma);
            list2.add(ma);
        });
        try {
            EasyExcelUtil.writeExcel(response,MediaOrderRecordsED.class,list2,"媒体广告列表");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "根据广告adId查询")
    @GetMapping("/getList/{adId}")
    public Result getList(@PathVariable("adId") Long adId){
        return ResultUtil.success(mediaOrderdRecorService.getList(adId));
    }

    @ApiOperation(value = "根据预约人id查询")
    @GetMapping("/getListByAgentId/{agentId}")
    public Result selectListByAgentId(@PathVariable("agentId") Long agentId){
        return ResultUtil.success(mediaOrderdRecorService.selectListByAgentId(agentId));
    }

    @ApiOperation(value = "查询某个状态的次数")
    @GetMapping("count")
    public Result selectNum(MediaOrderRecordQuery mediaOrderRecordQuery){
        return ResultUtil.success(mediaOrderdRecorService.selectNum(mediaOrderRecordQuery));
    }

    @ApiOperation(value = "新增/修改")
    @PostMapping("save")
    public Result save(@SysInfo SysAuth auth,MediaOrderdRecordDto mediaOrderdRecordDto){
        mediaOrderdRecorService.save(mediaOrderdRecordDto,auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "改变广告记录表状态")
    @PostMapping("changeRecordStatus")
    public Result changeRecordStatus(MediaOrderdRecordStatusDto mediaOrderdRecordStatusDto){
        mediaOrderdRecorService.changeRecordStatus(mediaOrderdRecordStatusDto.getId(),mediaOrderdRecordStatusDto.getStatus().getValue());
        return ResultUtil.success();
    }

    @ApiOperation(value = "投放广告二期项目过期，不在有投放按钮")
    @GetMapping("advertising/{adId}")
    @Deprecated
    public Result advertising(@PathVariable("adId")Long adId){
        mediaOrderdRecorService.advertising(adId);
        return ResultUtil.success();
    }
    @ApiOperation(value = "付款以后处理广告为失败处理")
    @GetMapping("handling/{id}")
    public Result handling(@PathVariable("id")Long id){
        MediaOrderdRecord check1=  mediaOrderdRecorService.getById(id);
        if(check1.getStatus().intValue()!= AppointmentEunm.STATUS7.getValue().intValue()){
            return  ResultUtil.error("不是已付款状态，不允许调整");
        }
        MediaOrderdRecord record=new MediaOrderdRecord();
        record.setId(id);
        record.setHandling(WhetherEnum.YES.getValue());
        return mediaOrderdRecorService.updateById(record)?ResultUtil.success():ResultUtil.error();
    }
    @ApiOperation(value = "批量修改状态-用于批量已收款、已付款、已排版")
    @PostMapping("batchUpdateStatus")
    public Result batchUpdateStatus(@RequestBody MediaOrderdRecordDto mediaOrderdRecordDto){
        mediaOrderdRecorService.batchUpdateStatus(mediaOrderdRecordDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据预约人信息")
    @GetMapping("/info/{id}/{agentId}")
    public Result selectInfo(@PathVariable("id") Long id,@PathVariable("agentId") Long agentId){
        MediaOrderRecordDetilVo m = mediaOrderdRecorService.lastApplyTime(id,agentId);
        Map map = new HashMap<String,Object>();
        Result<AgentVo> result = agentServiceRpc.selectById(agentId);
        if(result.isSuccess() && ObjectUtil.isNotEmpty(result.getData())){
            AgentVo agentVo = result.getData();
            map.put("name",agentVo.getAgentName());
            map.put("mobile",agentVo.getMobile());
            map.put("wechatNum",agentVo.getWechatNum());
            if(ObjectUtil.isNotEmpty(m)){
               SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
               map.put("lastApplyTime", sdf.format(m.getCreatedTime()));
            }else{
                map.put("lastApplyTime","");
            }
            return ResultUtil.success(map);
        }
        return ResultUtil.error("查询信息失败");
    }
}
